package com.huangpeng;

import com.huangpeng.designmodel.adapter.AudioPlayer;
import com.huangpeng.designmodel.builder.GoodsBuilder;
import com.huangpeng.designmodel.builder.ReceiptBuilder;
import com.huangpeng.designmodel.builder.TaxBuilder;
import com.huangpeng.designmodel.decorate.GameRole;
import com.huangpeng.designmodel.decorate.GameRoleDecorator;
import com.huangpeng.designmodel.decorate.PersonGameRoleDecorator;
import com.huangpeng.designmodel.decorate.Role;
import com.huangpeng.designmodel.enjoyyuan.Circle;
import com.huangpeng.designmodel.enjoyyuan.ShapeFactory;
import com.huangpeng.designmodel.entity.Goods;
import com.huangpeng.designmodel.entity.Receipt;
import com.huangpeng.designmodel.entity.Tax;
import com.huangpeng.designmodel.observer.Message;
import com.huangpeng.designmodel.observer.Supervisor1Observer;
import com.huangpeng.designmodel.observer.Supervisor2Observer;
import com.huangpeng.designmodel.observer.SupervisorObserver;
import com.huangpeng.designmodel.responsibility.ReceiptHandleChain;
import com.huangpeng.designmodel.strategy.IReceiptHandleStrategy;
import com.huangpeng.designmodel.strategy.ReceiptHandleStrategyFactory;
import com.huangpeng.designmodel.strategy.ReceiptStrategyContext;
import com.huangpeng.utils.CommonUtil;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@SpringBootApplication
public class DesignmodelApplication {

    public static void main(String[] args) {
        SpringApplication.run(DesignmodelApplication.class, args);

    }

    //策略模式+Map字典测试
    @Test
    public void test(){
        //模拟回执
        List<Receipt> receiptList = ReceiptBuilder.generateReceiptList();
        //策略上下文
        ReceiptStrategyContext receiptStrategyContext = new ReceiptStrategyContext();
        receiptList.forEach(s -> {
            //获取并设置策略
            IReceiptHandleStrategy receiptHandleStrategy = ReceiptHandleStrategyFactory.getReceiptHandleStrategy(s.getReceiptType());
            receiptStrategyContext.setReceiptHandleStrategy(receiptHandleStrategy);
            //执行策略
            receiptStrategyContext.handleReceipt(s);
        });
    }
    //责任链模式测试
    @Test
    public void test1(){
        //模拟回执
        List<Tax> taxList = TaxBuilder.generateReceiptList();
        List<Goods> goodsList = GoodsBuilder.generateReceiptList();
        taxList.forEach(s -> {
            //回执处理链对象
            ReceiptHandleChain receiptHandleChain = new ReceiptHandleChain();
            receiptHandleChain.handleReceipt(s,goodsList);
        });
    }

    //装饰者模式测试
    @Test
    public void test2(){
        GameRole role = new Role();
        GameRoleDecorator personRole = new PersonGameRoleDecorator(role);
        GameRoleDecorator personRole2 = new PersonGameRoleDecorator(personRole);
        System.out.println(role.person());
        System.out.println(personRole.person());
        System.out.println(personRole2.person());
    }

    //享元模式测试
    private static final String colors[] = {"Red", "Green", "Blue", "White", "Black"};
    @Test
    public void test3(){
        for (int i = 0; i < 10; i++) {
            Circle circle = (Circle) ShapeFactory.getCircle(getRandomColor());
            circle.setX(getRandomX());
            circle.setY(getRandomY());
            circle.setRadius(100);
            circle.draw();
        }
    }

    //适配器模式测试
    @Test
    public void test4(){
        AudioPlayer audioPlayer = new AudioPlayer();
        audioPlayer.play("mp3", "beyond the horizon.mp3");
        audioPlayer.play("mp4", "alone.mp4");
        audioPlayer.play("vlc", "far far away.vlc");
        audioPlayer.play("avi", "mind me.avi");
    }

    //观察者模式测试
    @Test
    public void test5(){
        Message message = new Message();
        new SupervisorObserver(message);
        new Supervisor1Observer(message);
        new Supervisor2Observer(message);

        String msg = "java从入门到放弃";
        System.out.println("上一条消息：" + msg);
        message.setMsg(msg);
        msg = "mysql从入门到删库跑路";
        System.out.println("当前同步消息：" + msg);
        message.setMsg(msg);
    }

    @Test
    public void test6(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        System.out.println(calendar.getTime());
        String date = CommonUtil.parseDateStr(calendar.getTime(),"yyyy-MM-dd");
        System.out.println(CommonUtil.parseDateStr(calendar.getTime(),"yyyy-MM-dd"));
        System.out.println(CommonUtil.parseDate(date,"yyyy-MM-dd"));
    }

    private int getRandomY() {
        return (int) (Math.random()*100);
    }

    private int getRandomX() {
        return (int) (Math.random()*100);
    }

    private String getRandomColor() {
        return colors[(int)(Math.random()*colors.length)];
    }
}
